{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Initialization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# For Colab only!\n",
    "\n",
    "try:\n",
    "  # %tensorflow_version only exists in Colab.\n",
    "  %tensorflow_version 2.x\n",
    "except Exception:\n",
    "  pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.1.0\n",
      "WARNING:tensorflow:From <ipython-input-3-2e82a26757ae>:2: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use `tf.config.list_physical_devices('GPU')` instead.\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(tf.__version__)\n",
    "print(tf.test.is_gpu_available())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.4.0\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(torch.__version__)\n",
    "print(torch.cuda.is_available())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Basic\n",
    "\n",
    "elementwise\n",
    "\n",
    "**\"  + - * /  \"**\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(\n",
      "[[1.2716367 1.8942944 0.703163  0.8952806]\n",
      " [1.2450049 1.2213507 1.1603341 0.7730472]\n",
      " [0.9520465 1.680704  0.5407109 0.9664177]], shape=(3, 4), dtype=float32)\n",
      "(3, 4)\n",
      "tf.Tensor(\n",
      "[[-0.6214237  -0.04250443 -0.32829726 -0.61047864]\n",
      " [-0.64805555 -0.71544814  0.12887383 -0.73271203]\n",
      " [-0.94101393 -0.2560948  -0.49074936 -0.53934157]], shape=(3, 4), dtype=float32)\n",
      "(3, 4)\n",
      "tf.Tensor(\n",
      "[[0.30772313 0.8966361  0.09666479 0.10721079]\n",
      " [0.2825153  0.24495785 0.3324417  0.01518377]\n",
      " [0.00522134 0.6897953  0.01288334 0.16076845]], shape=(3, 4), dtype=float32)\n",
      "(3, 4)\n",
      "tf.Tensor(\n",
      "[[0.34347185 0.95610857 0.36343208 0.18914177]\n",
      " [0.3153356  0.26120552 1.2498862  0.02678727]\n",
      " [0.00582791 0.7355484  0.0484377  0.2836284 ]], shape=(3, 4), dtype=float32)\n",
      "(3, 4)\n"
     ]
    }
   ],
   "source": [
    "#  \"+ - * /\"\n",
    "#  add subtract multiply div\n",
    "a = tf.random.uniform([3,4])\n",
    "b = tf.random.uniform([4])\n",
    "\n",
    "print(a+b)\n",
    "print(tf.add(a,b).shape)\n",
    "\n",
    "print(a-b)\n",
    "print(tf.subtract(a,b).shape)\n",
    "\n",
    "print(a*b)\n",
    "print(tf.multiply(a,b).shape)\n",
    "\n",
    "print(a/b)\n",
    "print(tf.divide(a,b).shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.7799, 0.8954, 1.6239, 0.4891],\n",
      "        [0.1286, 0.7415, 1.5803, 0.7019],\n",
      "        [0.6216, 1.2015, 1.6686, 0.7317]])\n",
      "torch.Size([3, 4])\n",
      "tensor([[ 0.6811,  0.4708, -0.3122,  0.4146],\n",
      "        [ 0.0298,  0.3170, -0.3558,  0.6275],\n",
      "        [ 0.5227,  0.7769, -0.2675,  0.6572]])\n",
      "torch.Size([3, 4])\n",
      "tensor([[0.0361, 0.1450, 0.6349, 0.0168],\n",
      "        [0.0039, 0.1124, 0.5927, 0.0247],\n",
      "        [0.0283, 0.2100, 0.6781, 0.0259]])\n",
      "torch.Size([3, 4])\n",
      "tensor([[14.7841,  3.2177,  0.6775, 12.1368],\n",
      "        [ 1.6028,  2.4930,  0.6325, 17.8541],\n",
      "        [11.5792,  4.6597,  0.7236, 18.6536]])\n",
      "torch.Size([3, 4])\n"
     ]
    }
   ],
   "source": [
    "#  \"+ - * /\"\n",
    "#  add sub mul div\n",
    "a = torch.rand(3,4)\n",
    "b = torch.rand(4)\n",
    "\n",
    "print(a+b)\n",
    "print(torch.add(a,b).shape)\n",
    "\n",
    "print(a-b)\n",
    "print(torch.sub(a,b).shape)\n",
    "\n",
    "print(a*b)\n",
    "print(torch.mul(a,b).shape)\n",
    "\n",
    "print(a/b)\n",
    "print(torch.div(a,b).shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### MatMul\n",
    "1. @\n",
    "2. matmul\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "cell_style": "split",
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(\n",
      "[[1.0291786  0.7188834  0.77422404]\n",
      " [1.566424   0.9554346  1.0769386 ]\n",
      " [1.9096622  1.3246509  1.5444131 ]], shape=(3, 3), dtype=float32)\n",
      "tf.Tensor(\n",
      "[[1.0291786  0.7188834  0.77422404]\n",
      " [1.566424   0.9554346  1.0769386 ]\n",
      " [1.9096622  1.3246509  1.5444131 ]], shape=(3, 3), dtype=float32)\n",
      "(2, 256)\n"
     ]
    }
   ],
   "source": [
    "a = tf.random.uniform([3,4])\n",
    "b = tf.random.uniform([4,3])\n",
    "\n",
    "print(a@b)\n",
    "print(tf.matmul(a,b))\n",
    "\n",
    "# Example: image x [2, 784] -> [2, 256]\n",
    "# w: [in, out]\n",
    "# b: [out]\n",
    "\n",
    "x = tf.random.uniform([2,784])\n",
    "w = tf.random.uniform([784,256])\n",
    "b = tf.zeros([256])\n",
    "\n",
    "out = x@w + b\n",
    "print(out.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.8665, 0.8029, 1.3531],\n",
      "        [0.7052, 0.8495, 1.0810],\n",
      "        [0.8507, 0.6912, 1.3413]])\n",
      "tensor([[0.8665, 0.8029, 1.3531],\n",
      "        [0.7052, 0.8495, 1.0810],\n",
      "        [0.8507, 0.6912, 1.3413]])\n",
      "torch.Size([2, 256])\n"
     ]
    }
   ],
   "source": [
    "a = torch.rand(3,4)\n",
    "b = torch.rand(4,3)\n",
    "\n",
    "print(a@b)\n",
    "print(torch.matmul(a,b))\n",
    "\n",
    "# Example: image x [2, 784] -> [2, 256]\n",
    "# w: [out, in],  w.t() transpose \n",
    "# b: [out]\n",
    "\n",
    "x = torch.rand(2, 784)\n",
    "w = torch.rand(256, 784)\n",
    "b = torch.zeros(256)\n",
    "\n",
    "out = x@w.t() + b\n",
    "print(out.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Dim over 2\n",
    "\n",
    "Only MatMul the last two dimentions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 3, 28, 28)\n",
      "(2, 3, 28, 28)\n",
      "(2, 3, 28, 14)\n"
     ]
    }
   ],
   "source": [
    "a = tf.random.uniform([2,3,28,12])\n",
    "b = tf.random.uniform([2,3,12,28])\n",
    "\n",
    "print((a@b).shape)\n",
    "print(tf.matmul(a,b).shape)\n",
    "\n",
    "# automatic broadcasting\n",
    "# c [12, 14] -> [1,1,12,14] ->[2,3,12,14]\n",
    "c = tf.random.uniform([12,14])\n",
    "print((a@c).shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([2, 3, 28, 28])\n",
      "torch.Size([2, 3, 28, 28])\n",
      "torch.Size([2, 3, 28, 14])\n"
     ]
    }
   ],
   "source": [
    "a = torch.rand(2,3,28,12)\n",
    "b = torch.rand(2,3,12,28)\n",
    "\n",
    "print((a@b).shape)\n",
    "print(torch.matmul(a,b).shape)\n",
    "\n",
    "# automatic broadcasting\n",
    "# c [12, 14] -> [1,1,12,14] ->[2,3,12,14]\n",
    "c = torch.rand(12,14)\n",
    "print((a@c).shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center"
   },
   "source": [
    "### Power and log\n",
    "elemente-wise: pow sqll **"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(\n",
      "[[81. 81. 81.]\n",
      " [81. 81. 81.]\n",
      " [81. 81. 81.]], shape=(3, 3), dtype=float32)\n",
      "tf.Tensor(\n",
      "[[81. 81. 81.]\n",
      " [81. 81. 81.]\n",
      " [81. 81. 81.]], shape=(3, 3), dtype=float32)\n",
      "tf.Tensor(\n",
      "[[3. 3. 3.]\n",
      " [3. 3. 3.]\n",
      " [3. 3. 3.]], shape=(3, 3), dtype=float32)\n",
      "tf.Tensor(\n",
      "[[3. 3. 3.]\n",
      " [3. 3. 3.]\n",
      " [3. 3. 3.]], shape=(3, 3), dtype=float32)\n",
      "tf.Tensor(\n",
      "[[2. 2. 2.]\n",
      " [2. 2. 2.]\n",
      " [2. 2. 2.]], shape=(3, 3), dtype=float32) tf.Tensor(\n",
      "[[7.389056 7.389056 7.389056]\n",
      " [7.389056 7.389056 7.389056]\n",
      " [7.389056 7.389056 7.389056]], shape=(3, 3), dtype=float32) tf.Tensor(\n",
      "[[2. 2. 2.]\n",
      " [2. 2. 2.]\n",
      " [2. 2. 2.]], shape=(3, 3), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "a = tf.fill([3,3],9.0)\n",
    "print(a**2)\n",
    "print(tf.pow(a, 2))\n",
    "print(a**0.5)\n",
    "print(tf.sqrt(a))\n",
    "\n",
    "b = tf.ones([3,3])*2\n",
    "c = tf.exp(b)\n",
    "d = tf.math.log(c)\n",
    "print(b, c, d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[81., 81., 81.],\n",
      "        [81., 81., 81.],\n",
      "        [81., 81., 81.]])\n",
      "tensor([[81., 81., 81.],\n",
      "        [81., 81., 81.],\n",
      "        [81., 81., 81.]])\n",
      "tensor([[3., 3., 3.],\n",
      "        [3., 3., 3.],\n",
      "        [3., 3., 3.]])\n",
      "tensor([[3., 3., 3.],\n",
      "        [3., 3., 3.],\n",
      "        [3., 3., 3.]])\n",
      "tensor([[2., 2., 2.],\n",
      "        [2., 2., 2.],\n",
      "        [2., 2., 2.]]) tensor([[7.3891, 7.3891, 7.3891],\n",
      "        [7.3891, 7.3891, 7.3891],\n",
      "        [7.3891, 7.3891, 7.3891]]) tensor([[2., 2., 2.],\n",
      "        [2., 2., 2.],\n",
      "        [2., 2., 2.]])\n"
     ]
    }
   ],
   "source": [
    "a = torch.full([3,3],9)\n",
    "print(a**2)\n",
    "print(torch.pow(a,2))\n",
    "print(a**0.5)\n",
    "print(torch.sqrt(a))\n",
    "\n",
    "b = torch.ones([3,3])*2\n",
    "c = torch.exp(b)\n",
    "d = torch.log(c)\n",
    "print(b, c, d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "center"
   },
   "source": [
    "### Approximation\n",
    "\n",
    "* floor() ceil()\n",
    "* round()\n",
    "* trunc() frac()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(\n",
      "[[9.6797943e-04 9.5370598e+00 3.1854320e+00]\n",
      " [2.0480025e+00 4.2486677e+00 3.0384183e+00]\n",
      " [2.9459715e+00 3.3821106e-01 5.6323252e+00]], shape=(3, 3), dtype=float32)\n",
      "tf.Tensor(\n",
      "[[0. 9. 3.]\n",
      " [2. 4. 3.]\n",
      " [2. 0. 5.]], shape=(3, 3), dtype=float32)\n",
      "tf.Tensor(\n",
      "[[ 1. 10.  4.]\n",
      " [ 3.  5.  4.]\n",
      " [ 3.  1.  6.]], shape=(3, 3), dtype=float32)\n",
      "tf.Tensor(\n",
      "[[ 0. 10.  3.]\n",
      " [ 2.  4.  3.]\n",
      " [ 3.  0.  6.]], shape=(3, 3), dtype=float32)\n",
      "[[0. 9. 3.]\n",
      " [2. 4. 3.]\n",
      " [2. 0. 5.]]\n",
      "[[0.00096798 0.5370598  0.18543196]\n",
      " [0.04800248 0.24866772 0.03841829]\n",
      " [0.9459715  0.33821106 0.6323252 ]]\n"
     ]
    }
   ],
   "source": [
    "a = tf.random.uniform([3,3],0,10)\n",
    "print(a)\n",
    "print(tf.floor(a))\n",
    "print(tf.math.ceil(a))\n",
    "print(tf.math.round(a))\n",
    "print(np.trunc(a.numpy()))\n",
    "print(np.modf(a.numpy())[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "cell_style": "split",
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[5.2186, 6.0076, 3.3427],\n",
      "        [4.3957, 8.9697, 7.8908],\n",
      "        [3.8569, 7.8350, 7.2415]])\n",
      "tensor([[5., 6., 3.],\n",
      "        [4., 8., 7.],\n",
      "        [3., 7., 7.]])\n",
      "tensor([[6., 7., 4.],\n",
      "        [5., 9., 8.],\n",
      "        [4., 8., 8.]])\n",
      "tensor([[5., 6., 3.],\n",
      "        [4., 9., 8.],\n",
      "        [4., 8., 7.]])\n",
      "tensor([[5., 6., 3.],\n",
      "        [4., 8., 7.],\n",
      "        [3., 7., 7.]])\n",
      "tensor([[0.2186, 0.0076, 0.3427],\n",
      "        [0.3957, 0.9697, 0.8908],\n",
      "        [0.8569, 0.8350, 0.2415]])\n"
     ]
    }
   ],
   "source": [
    "a = torch.rand([3,3])*10\n",
    "print(a)\n",
    "print(a.floor())\n",
    "print(a.ceil())\n",
    "print(a.round())\n",
    "print(a.trunc())\n",
    "print(a.frac())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Statistics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split"
   },
   "source": [
    "* tf.norm\n",
    "* tf.reduce_min/max, tf.argmax/argmin \n",
    "* tf.reduce_mean/sum\n",
    "* tf.math.top_k\n",
    "* tf.unique"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_style": "split"
   },
   "source": [
    "* norm\n",
    "* max, min, argmin, argmax \n",
    "* sum mean\n",
    "* kthvalue, topk\n",
    "* prod\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(\n",
      "[[1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]], shape=(2, 4), dtype=float64)\n",
      "tf.Tensor(\n",
      "[[[1. 1.]\n",
      "  [1. 1.]]\n",
      "\n",
      " [[1. 1.]\n",
      "  [1. 1.]]], shape=(2, 2, 2), dtype=float64)\n",
      "8.0\n",
      "2.8284271247461903\n",
      "tf.Tensor([1.41421356 1.41421356 1.41421356 1.41421356], shape=(4,), dtype=float64)\n",
      "tf.Tensor(\n",
      "[[0 1 2 3]\n",
      " [4 5 6 7]], shape=(2, 4), dtype=int64)\n",
      "tf.Tensor(0, shape=(), dtype=int64)\n",
      "tf.Tensor(3, shape=(), dtype=int64)\n",
      "tf.Tensor(7, shape=(), dtype=int64)\n",
      "tf.Tensor(0, shape=(), dtype=int64)\n",
      "tf.Tensor(28, shape=(), dtype=int64)\n",
      "tf.Tensor([1 1 1 1], shape=(4,), dtype=int64)\n",
      "tf.Tensor([0 0 0 0], shape=(4,), dtype=int64)\n",
      "tf.Tensor([3 1 3 5], shape=(4,), dtype=int64)\n",
      "tf.Tensor([0.9782531  0.78539133 0.89602935 0.90916646], shape=(4,), dtype=float32)\n",
      "tf.Tensor(\n",
      "[[0.9782531 ]\n",
      " [0.78539133]\n",
      " [0.89602935]\n",
      " [0.90916646]], shape=(4, 1), dtype=float32)\n",
      "TopKV2(values=<tf.Tensor: id=572, shape=(4, 5), dtype=float32, numpy=\n",
      "array([[0.9782531 , 0.92342305, 0.92287254, 0.7878777 , 0.65372133],\n",
      "       [0.78539133, 0.6969075 , 0.67203116, 0.63833797, 0.44773853],\n",
      "       [0.89602935, 0.8353468 , 0.82580435, 0.6044351 , 0.59917295],\n",
      "       [0.90916646, 0.8699833 , 0.85012865, 0.82335556, 0.42636704]],\n",
      "      dtype=float32)>, indices=<tf.Tensor: id=573, shape=(4, 5), dtype=int32, numpy=\n",
      "array([[3, 4, 2, 6, 9],\n",
      "       [1, 9, 4, 6, 8],\n",
      "       [3, 5, 8, 0, 1],\n",
      "       [5, 4, 3, 9, 0]], dtype=int32)>)\n"
     ]
    }
   ],
   "source": [
    "a = tf.fill([8], 1)\n",
    "a = tf.cast(a, tf.float64)\n",
    "b = tf.reshape(a, [2,4])\n",
    "c = tf.reshape(a, [2,2,2])\n",
    "\n",
    "print(b)\n",
    "print(c)\n",
    "\n",
    "print(tf.norm(b,ord=1).numpy())\n",
    "print(tf.norm(b,ord=2).numpy())\n",
    "print(tf.norm(b,ord=2, axis=0))\n",
    "\n",
    "a = tf.constant(np.arange(8))\n",
    "a = tf.reshape(a,[2,4])\n",
    "print(a)\n",
    "\n",
    "print(tf.reduce_min(a))\n",
    "print(tf.reduce_mean(a))\n",
    "print(tf.reduce_max(a))\n",
    "print(tf.reduce_prod(a))\n",
    "print(tf.reduce_sum(a))\n",
    "print(tf.argmax(a, 0))\n",
    "print(tf.argmin(a, 0))\n",
    "\n",
    "# Example: x is the output of 4 MINST images\n",
    "x = tf.random.uniform([4,10])\n",
    "print(tf.argmax(x,1))\n",
    "print(tf.reduce_max(x, axis=1))\n",
    "print(tf.reduce_max(x, axis=1, keepdims=True))\n",
    "print(tf.math.top_k(x, k=5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1., 1., 1., 1.],\n",
      "        [1., 1., 1., 1.]])\n",
      "tensor([[[1., 1.],\n",
      "         [1., 1.]],\n",
      "\n",
      "        [[1., 1.],\n",
      "         [1., 1.]]])\n",
      "tensor(8.)\n",
      "tensor(2.8284)\n",
      "tensor([2., 2., 2., 2.])\n",
      "tensor([2., 2.])\n",
      "tensor([[2., 2.],\n",
      "        [2., 2.]])\n",
      "tensor([[1.4142, 1.4142],\n",
      "        [1.4142, 1.4142]])\n",
      "torch.Size([2, 2])\n",
      "tensor([[0., 1., 2., 3.],\n",
      "        [4., 5., 6., 7.]])\n",
      "tensor(0.) tensor(7.) tensor(3.5000) tensor(0.)\n",
      "tensor(28.)\n",
      "tensor(7) tensor(0)\n",
      "tensor([1, 1, 1, 1]) tensor([3, 3])\n",
      "tensor([6, 8, 0, 1])\n",
      "torch.return_types.max(\n",
      "values=tensor([0.7343, 0.9775, 0.6649, 0.9459]),\n",
      "indices=tensor([6, 8, 0, 1]))\n",
      "torch.return_types.max(\n",
      "values=tensor([[0.7343],\n",
      "        [0.9775],\n",
      "        [0.6649],\n",
      "        [0.9459]]),\n",
      "indices=tensor([[6],\n",
      "        [8],\n",
      "        [0],\n",
      "        [1]]))\n",
      "torch.return_types.topk(\n",
      "values=tensor([[0.7343, 0.7134, 0.6342],\n",
      "        [0.9775, 0.8714, 0.7993],\n",
      "        [0.6649, 0.6179, 0.5973],\n",
      "        [0.9459, 0.9299, 0.4478]]),\n",
      "indices=tensor([[6, 8, 4],\n",
      "        [8, 1, 5],\n",
      "        [0, 1, 3],\n",
      "        [1, 2, 5]]))\n",
      "torch.return_types.kthvalue(\n",
      "values=tensor([0.5380, 0.7642, 0.5802, 0.4336]),\n",
      "indices=tensor([1, 2, 2, 4]))\n"
     ]
    }
   ],
   "source": [
    "a = torch.full([8], 1)\n",
    "b = a.reshape([2,4])\n",
    "c = a.reshape([2,2,2])\n",
    "\n",
    "print(b)\n",
    "print(c)\n",
    "\n",
    "# torch.norm()\n",
    "print(b.norm(1))\n",
    "print(c.norm(2))\n",
    "\n",
    "print(b.norm(1, dim = 0))\n",
    "print(b.norm(2, dim = 1))\n",
    "\n",
    "print(c.norm(1, dim = 0))\n",
    "print(c.norm(2, dim = 1))\n",
    "print(c.norm(2, dim = 2).shape)\n",
    "\n",
    "a = torch.arange(8).reshape(2,4).float()\n",
    "print(a)\n",
    "print(a.min(), a.max(), a.mean(), a.prod())\n",
    "print(a.sum())\n",
    "print(a.argmax(), a.argmin())\n",
    "# torch.argmax()\n",
    "print(a.argmax(dim=0), a.argmax(dim=1))\n",
    "\n",
    "# Example: x is the output of 4 MINST images\n",
    "x = torch.rand(4,10)\n",
    "print(x.argmax(dim=1))\n",
    "print(x.max(dim =1))\n",
    "print(x.max(dim=1, keepdim=True))\n",
    "# torch.topk()\n",
    "# torch.kthvalue()\n",
    "print(x.topk(3,dim=1))\n",
    "print(x.kthvalue(7,dim=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Clamp\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(\n",
      "[[10.652033   5.9660015  2.4843903]\n",
      " [11.617971   3.1870842  5.1878815]], shape=(2, 3), dtype=float32)\n",
      "tf.Tensor(11.617971, shape=(), dtype=float32)\n",
      "tf.Tensor(2.4843903, shape=(), dtype=float32)\n",
      "tf.Tensor(\n",
      "[[10.         5.9660015  5.       ]\n",
      " [10.         5.         5.1878815]], shape=(2, 3), dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "grad = tf.random.uniform([2,3], minval=15)\n",
    "\n",
    "print(grad)\n",
    "\n",
    "print(tf.reduce_max(grad))\n",
    "print(tf.reduce_min(grad))\n",
    "\n",
    "print(tf.clip_by_value(grad, 5,10))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "cell_style": "split",
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[12.8346,  9.5469, 14.3791],\n",
      "        [12.7299,  2.6548,  5.8642]])\n",
      "tensor(14.3791)\n",
      "tensor(2.6548)\n",
      "tensor([[10.0000,  9.5469, 10.0000],\n",
      "        [10.0000,  5.0000,  5.8642]])\n",
      "tensor([[10.0000,  9.5469, 10.0000],\n",
      "        [10.0000,  5.0000,  5.8642]])\n"
     ]
    }
   ],
   "source": [
    "grad = torch.rand(2,3)*15\n",
    "\n",
    "print(grad)\n",
    "\n",
    "print(grad.max())\n",
    "print(grad.min())\n",
    "\n",
    "print(torch.clamp(grad, min=5, max=10))\n",
    "print(grad.clamp(5,10))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Compare\n",
    "\n",
    "* > < == != >= <=\n",
    "* equal\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(\n",
      "[[ True  True  True]\n",
      " [ True  True  True]\n",
      " [ True  True  True]], shape=(3, 3), dtype=bool)\n",
      "tf.Tensor(\n",
      "[[ True  True  True]\n",
      " [ True  True  True]\n",
      " [ True  True  True]], shape=(3, 3), dtype=bool)\n"
     ]
    }
   ],
   "source": [
    "x = tf.random.uniform([3,3],minval=0,maxval=2)\n",
    "# print(x)\n",
    "# print(x>1)\n",
    "\n",
    "y = x\n",
    "\n",
    "print(tf.equal(x,y))\n",
    "print(x == y)\n",
    "\n",
    "# result = tf.equal(x,y)\n",
    "# result = tf.cast(result, dtype=tf.int32)\n",
    "# print(tf.reduce_sum(result))\n",
    "\n",
    "# accuracy = tf.reduce_sum(result)/tf.size(tf.reshape(result,[-1]))\n",
    "# print(accuracy)\n",
    "\n",
    "# unique_element, unique_idx = tf.unique(tf.reshape(x,[-1]))\n",
    "# print(unique_idx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "cell_style": "split"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[False, False, False, False],\n",
      "        [False, False, False, False],\n",
      "        [False, False, False, False],\n",
      "        [False, False, False, False]])\n",
      "tensor([[True, True, True, True],\n",
      "        [True, True, True, True],\n",
      "        [True, True, True, True],\n",
      "        [True, True, True, True]])\n",
      "tensor([[True, True, True, True],\n",
      "        [True, True, True, True],\n",
      "        [True, True, True, True],\n",
      "        [True, True, True, True]])\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "x = torch.rand([4,4])\n",
    "print(x>1)\n",
    "\n",
    "y = x\n",
    "\n",
    "print(torch.eq(x,y))\n",
    "print(x == y)\n",
    "print(torch.equal(x,y))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
